﻿using KWT.DRG_DIP.DO.System;
using Microsoft.EntityFrameworkCore;
using Namotion.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace KWT.DRG_DIP.DB
{
    public class EFContext : DbContext
    {
        public EFContext()
        {
        }

        public EFContext(DbContextOptions<EFContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var ass = Assembly.Load("KWT.DRG-DIP.PO").ExportedTypes;
            var types = ass.Where(x => typeof(Audit).IsAssignableFrom(x));

            foreach (var type in types)
            {
                var parameter = Expression.Parameter(type);

                var propertyMethodInfo = typeof(EF).GetMethod("Property")?.MakeGenericMethod(typeof(bool));

                var isDeletedProperty = Expression.Call(propertyMethodInfo, parameter, Expression.Constant("IsDel"));

                BinaryExpression compareExpression = Expression.MakeBinary(ExpressionType.Equal, isDeletedProperty, Expression.Constant(false));

                var lambdaExpression = Expression.Lambda(compareExpression, parameter);

                modelBuilder.Entity(type).HasQueryFilter(lambdaExpression);
            }


            foreach (var type in ass)
            {
                string class_summary = type.GetXmlDocsSummary();
                modelBuilder.Entity(type).ToTable(x => x.HasComment(class_summary));
                foreach (var prop in type.GetProperties())
                {
                    string prop_summary = prop.GetXmlDocsSummary();
                    modelBuilder.Entity(type).Property(prop.Name).HasComment(prop_summary);
                }
            }

            var entityTypes = modelBuilder.Model.GetEntityTypes();

            var props = entityTypes.SelectMany(t => t.GetProperties());

            foreach (var property in props.Where(p => p.ClrType == typeof(string)))
            {
                if (property.GetMaxLength() == null && !property.IsPrimaryKey())
                {
                    property.SetMaxLength(256);
                    property.SetDefaultValue("");
                }
            }
            foreach (var property in props.Where(p => p.ClrType == typeof(DateTime)))
            {
                property.SetDefaultValueSql("now()");
                property.SetPrecision(0);
            }

            foreach (var property in props.Where(p => p.ClrType == typeof(DateTime?)))
            {
                property.SetColumnType("datetime");
                property.SetPrecision(0);
            }

            foreach (var property in props.Where(p => p.ClrType == typeof(bool)))
            {
                property.SetDefaultValue(0);
            }

            foreach (var property in props.Where(p => p.ClrType == typeof(double)))
            {
                property.SetColumnType("decimal(9,2)");
            }
            foreach (var property in props.Where(p => p.ClrType == typeof(decimal)))
            {
                if (property.GetPrecision()>0)
                {
                    property.SetColumnType($"decimal(18,{property.GetPrecision()})");
                }
                else
                {
                    property.SetColumnType("decimal(18,2)");
                }
            }
        }
    }
}
